// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();

// 云函数入口函数
//查询帖子
exports.main = async (event, context) => {
  const openid = cloud.getWXContext().OPENID
  var num = event.num
  var page = event.page
  var schoolName = event.schoolName
  var locationName = event.locationName
  // var res =  await db.collection("jf_register").where({
  //   openid,
  // }).get()
  // res.data[0].mySchoolName
  // res.data[0].mylocationName
  var dataResult =  await db.collection("jf_info").orderBy("_updateTime","desc") .skip(num).limit(page).where({
    ispick:false,
    state:1,
    schoolName:schoolName,
    schoolLocation:locationName
    //付钱后 本校 才可查询
  }).get()
 
  var dataArr = dataResult.data
  if(dataArr.length>0)
  {
    for(var i=0;i < dataArr.length;i++)
    {

        var count = await db.collection("jf_dan").where({
          openid:openid,
          commonid:dataArr[i]._id
        }).count();
        if(count.total)
        {
          dataArr[i].isZan = false
        }
        else{
          dataArr[i].isZan = true
        }


        var count2 = await db.collection("jf_dan").where({
          commonid:dataArr[i]._id
        }).count();
        dataArr[i].zanSize = count2.total;

        if(openid==dataArr[i].openid){
          dataArr[i].delState=true
        }else{
          dataArr[i].delState=false
        }
        
    }
  }
  return dataArr;
}